package com.flower.handler;

import com.flower.exception.BusinessException;
import com.flower.exception.LoginException;
import com.flower.utils.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public AjaxResult handleBusinessException(BusinessException e) {
        log.error("业务异常: {}", e.getMessage());
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /**
     * 处理其他未知异常
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception e) {
        log.error("系统异常: ", e);
        return AjaxResult.error(e.getMessage());
    }
    @ExceptionHandler(value = RedisConnectionFailureException.class)
    public AjaxResult redisException(RedisConnectionFailureException e) {
        log.info("Redis连接异常  + {}", e.getMessage());
        return AjaxResult.error(401, "Redis连接异常");
    }

    @ExceptionHandler(value = LoginException.class)
    public AjaxResult loginException(LoginException e) {
        log.info("登录异常  + {}", e.getMsg());
        return AjaxResult.error(401, e.getMsg());
    }


} 